package com.sc.nft.controller;

import cn.hutool.core.util.StrUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.entity.dto.PrizeDrawDTO;
import com.sc.nft.entity.dto.RaffleRouletteDTO;
import com.sc.nft.entity.vo.NumberOfDrawsVO;
import com.sc.nft.entity.vo.PrizeDrawVO;
import com.sc.nft.entity.vo.RaffleRouletteVO;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.PrizeDrawActivityAwardService;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * 抽奖活动的奖品(PrizeDrawActivityAward)表控制层
 *
 * @author wwc
 * @since 2023-08-22 15:14:17
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("prizeDrawActivityAward")
@Api(tags = "抽奖奖励")
public class PrizeDrawActivityAwardController {

    /**
     * 服务对象
     */
    private final PrizeDrawActivityAwardService prizeDrawActivityAwardService;

    private final RedissonClient redissonClient;

    @PostMapping(value = "/raffleRoulette", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "抽奖轮盘", notes = "抽奖轮盘", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<RaffleRouletteVO> raffleRoulette(@Valid @RequestBody RaffleRouletteDTO raffleRouletteDTO) {
        return Result.success(prizeDrawActivityAwardService.raffleRoulette(raffleRouletteDTO, UserPool.getUser()));
    }

    @PostMapping(value = "/prizeDraw", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "抽奖", notes = "抽奖", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    @SneakyThrows
    public Result<PrizeDrawVO> prizeDraw(@Valid @RequestBody PrizeDrawDTO prizeDrawDTO) {
        String lockKey = StrUtil.format("prizeDraw:{}", prizeDrawDTO.getId());
        RLock lock = redissonClient.getLock(lockKey);
        PrizeDrawVO prizeDrawVO;
        if (!lock.tryLock(10, TimeUnit.SECONDS)) {
            throw new GlobalRunTimeException("网络好像不太好，稍后在试试吧");
        }
        try {
            prizeDrawVO = prizeDrawActivityAwardService.prizeDraw(prizeDrawDTO, UserPool.getUser());
        } finally {
            if (lock!=null) {
                lock.unlock();
            }
        }
        return Result.success(prizeDrawVO);
    }

    @PostMapping(value = "/numberOfDraws", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "抽奖次数", notes = "抽奖次数", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<NumberOfDrawsVO> numberOfDraws(@Valid @RequestBody RaffleRouletteDTO raffleRouletteDTO) {
        return Result.success(prizeDrawActivityAwardService.numberOfDraws(raffleRouletteDTO,UserPool.getUser()));
    }


}
